package org.lee.export.impl;

import org.lee.export.IPptExporter;
import org.lee.export.model.ApiResult;
import org.lee.export.model.PptVersion;
import org.lee.export.model.ResultCode;
import org.lee.export.model.SourceType;
import org.lee.export.utils.FileUtils;

import java.io.File;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author jackielee
 * @version 1.0
 * @description BasicPptExporter
 * @date 2021/5/6
 **/
public abstract class BasicPptExporter implements IPptExporter {

    public abstract PptVersion getVersion();

    /**
     * 导出单个ppt文件
     *
     * @param fileName 文件名
     * @param pathSave 保存目录
     * @return
     */
    public ApiResult export(String fileName, String pathSave) {
        File file = new File(fileName);
        if (!file.exists() || !file.isFile()) {
            return ApiResult.failure(ResultCode.INVALID, "not a file");
        }
        File[] files = {file};
        return doExports(Arrays.asList(files), pathSave);
    }

    /**
     * 导出多个ppt文件
     *
     * @param files    文件
     * @param pathSave 保存目录
     * @return
     */
    public ApiResult export(List<String> files, String pathSave) {
        List<File> fis = files.stream().map(f -> {
            File file = new File(f);
            if (!file.exists()) {
                return null;
            }
            return file;
        }).filter(f -> f != null).collect(Collectors.toList());
        return doExports(fis, pathSave);
    }

    /**
     * 导出单个或目录中的ppt文件
     *
     * @param path       源
     * @param pathSave   保存的目录
     * @param sourceType 指定传递的path类型为文件还是目录
     * @return
     */
    public ApiResult export(String path, String pathSave, SourceType sourceType) {
        if (sourceType == SourceType.FILE) {
            return export(path, pathSave);
        }
        List<File> files = getFiles(path);

        if (files.isEmpty()) {
            return ApiResult.failure(ResultCode.FAILURE, "the file not exists.");
        }
        return doExports(files, pathSave);
    }

    private ApiResult doExports(List<File> files, String pathSave) {
        File fSave = new File(pathSave);
        if (!fSave.exists()) {
            fSave.mkdir();
        }
        Map<String, List<String>> result = new Hashtable<>();
        for (File file : files) {
            List<String> pngs = doExport(file, pathSave);
            if (pngs.size() > 0) {
                result.put(file.getName(), pngs);
            }
        }

        return result.isEmpty() ? ApiResult.failure(ResultCode.FAILURE, "export the ppt failure") : ApiResult.success(result);
    }

    protected abstract List<String> doExport(File file, String pathSave);

    protected List<File> getFiles(String path) {
        File file = new File(path);
        List<File> files = new ArrayList<>();
        if (!file.exists()) {
            return files;
        }
        if (file.isDirectory()) {
            List<File> list = Arrays.asList(file.listFiles()).stream().filter(f -> isPptVersion(f)).collect(Collectors.toList());
            files.addAll(list);
        } else {
            if (isPptVersion(file)) {
                files.add(file);
            }
        }
        return files;
    }

    protected boolean isPptVersion(File file) {
        return FileUtils.getExt(file).equalsIgnoreCase(getVersion().getExtension());
    }
}
